#!/bin/bash

# 二进制包部署docker
dnf_docker()
{
WORK_DIR=$(cd $(dirname $0);pwd)
tar -zxf $WORK_DIR/docker-20.10.5.tgz
chmod 755 -R docker && mv docker/* /usr/bin

# 开启路由转发
sed -i 's/net.ipv4.ip_forward=0/net.ipv4.ip_forward=1/' /etc/sysctl.conf
sysctl -p

cat > /etc/systemd/system/docker.service << EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com

[Service]
Type=notify
ExecStart=/usr/bin/dockerd --data-root /data/docker
ExecReload=/bin/kill -s HUP \$MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

StartLimitBurst=3
StartLimitInterval=60s

LimitNOFILE=1024576
LimitNPROC=1024576
LimitCORE=infinity

Delegate=yes

KillMode=process
OOMScoreAdjust=-500

[Install]
WantedBy=multi-user.target
EOF

chmod +x /etc/systemd/system/docker.service

mkdir -p /etc/docker
cat <<EOF | tee /etc/docker/daemon.json
{
    "insecure-registries": ["10.196.14.67:5000"],
    "exec-opts":["native.cgroupdriver=systemd"],
    "log-driver":"json-file",
    "log-opts":{"max-size":"100m","max-file":"5"},
    "storage-driver":"overlay2",
    "storage-opts":["overlay2.override_kernel_check=true"]
}
EOF

systemctl daemon-reload

DOCKER_INSTALL_START_STATUS=$(systemctl start docker &> /dev/null && echo 'yes'|| echo 'no')
if [[ ${DOCKER_INSTALL_START_STATUS} = 'yes' ]];then
  echo -e "\033[1m\033[40;32mDocker 服务安装完成!\033[0m"
elif [[ ${DOCKER_INSTALL_START_STATUS} = 'no' ]];then
  echo -e "\033[1m\033[40;31mDocker 服务安装失败!\033[1m \033[40;33m(请手动安装Docker环境)\033[0m"
  exit 1
fi

systemctl enable docker
docker -v
}

# 部署compose
dnf_compose()
{
mv docker-compose-linux-x86_64 docker-compose
chmod +x docker-compose && mv ./docker-compose /usr/bin/
docker-compose -v
}

# 解决 docker Tab 命令无法补全问题
dnf_Tab()
{
mv ./bash/docker /etc/bash_completion.d/
echo "请手动执行以下 source 命令"
echo "source /etc/bash_completion.d/docker"
}


dnf_docker
dnf_compose
dnf_Tab